SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO


CREATE PROCEDURE dbo.[DnnProduct_PWSyncDashBoard_LastSyncStatusesDashBoardCount]
	@WhereClause nvarchar(1000)
AS
BEGIN
	DECLARE @SQLString NVARCHAR(1000)

	
	SET @SQLString = N'WITH stat AS (SELECT s.*, ROW_NUMBER() OVER(PARTITION BY s.DBName ORDER BY s.RunTime DESC) AS rk 
										FROM [DnnProduct_PWSyncDashBoard_LastSyncStatuses] s),
							ItemRN AS (SELECT * FROM stat WHERE stat.rk = 1)
							SELECT count(LastSyncStatusesId)
							FROM ItemRN WHERE 1 = 1'
	

	IF (LEN(@WhereClause) > 0)
	BEGIN
		SET @SQLString = @SQLString + @WhereClause
	END
	
	EXECUTE sp_executesql @SQLString
END
GO
